﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Cms.Tests.Unit.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class MenuVersionTests
    {

        [TestMethod]
        public void GetRootItems_Success()
        {
            MenuVersion original = MenuTestsHelper.CreateVersion();
            MenuVersion clone = (MenuVersion)original.Clone();
            Assert.AreEqual(1, clone.GetRootItems().Count);
        }


        [TestMethod]
        public void CloneVersion_VerifyTreeStructure_Success()
        {
            MenuVersion original = MenuTestsHelper.CreateVersion();
            MenuVersion clone = (MenuVersion)original.Clone();
            Assert.AreEqual("Parent", clone.GetRootItems()[0].Translations[1].Name);
            Assert.AreEqual("Child", clone.GetRootItems()[0].SubItems[0].Translations[1].Name);
            Assert.AreEqual("GrandChild", clone.GetRootItems()[0].SubItems[0].SubItems[0].Translations[1].Name);
            Assert.IsNull(clone.GetRootItems()[0].Parent);
            Assert.AreSame(clone.GetRootItems()[0], clone.GetRootItems()[0].SubItems[0].Parent);
        }


        [TestMethod]
        public void GetItem_Success()
        {
            MenuVersion v = MenuTestsHelper.CreateVersion();
            Assert.AreEqual(3, v.GetItem(3).ID);
            Assert.AreEqual("GrandChild", v.GetItem(3).Translations[1].Name);
        }


        [TestMethod]
        public void CanItemBeNamedAndMovedTo_NoNamingConflict_Success()
        {
            MenuVersion v = MenuTestsHelper.CreateVersion();
            Assert.IsTrue(v.CanItemBeNamedAndMovedTo(2, 1, "Child", null));
        }


        [TestMethod]
        public void CanItemBeNamedAndMovedTo_NamingConflict_Failure()
        {
            MenuVersion v = MenuTestsHelper.CreateVersion();
            Assert.IsFalse(v.CanItemBeNamedAndMovedTo(2, 1, "Parent", null));
            Assert.IsFalse(v.CanItemBeNamedAndMovedTo(3, 1, "Child", v.GetRootItems()[0]));
        }


        [TestMethod]
        public void UpdateSortIndexes_MoveAllSubItemsToRoot_SortIndexIsOneTwoThree()
        {
            MenuVersion v = MenuTestsHelper.CreateVersion();
            MenuItem parent = v.Items[0];
            MenuItem child = v.Items[1];
            MenuItem grandChild = v.Items[2];
            // Move GrandChild first
            v.Items.Remove(grandChild);
            child.SubItems.Remove(grandChild);
            grandChild.Parent = null;
            v.Items.Insert(0, grandChild);
            // Move Child second
            v.Items.Remove(child);
            parent.SubItems.Remove(child);
            child.Parent = null;
            v.Items.Insert(1, child);
            v.UpdateSortIndexes();
            Assert.AreEqual(0, grandChild.SortIndex);
            Assert.AreEqual(1, child.SortIndex);
            Assert.AreEqual(2, parent.SortIndex);
        }

    }

}
